﻿using System;
using System.Text;
using System.Text.RegularExpressions;


namespace Portal
{
    public static class ArticleUtils
    {
        public static string GetSynopsisExtract(this string target, int count)
        {
            string logExtract = NoHTML(target);
            if (logExtract.Length < count)
            {
                return logExtract;
            }
            else
            {
                return logExtract.Substring(0, count);
            }
        }


        public static int GetStrLength(string str)
        {
            bool winntChinese = ("中国".Length == 2);
            if (!winntChinese)
            {
                return str.Length;
            }
            int l = str.Length;
            int T = l;
            for (int i = 1; i <= l; i++)
            {
                Encoding ascii = Encoding.ASCII;
                byte[] encodedBytes = ascii.GetBytes(str.Substring(i - 1, 1));
                int c = encodedBytes[0];
                if (c < 0)
                    c += 65536;
                if (c > 255)
                    T += 1;
            }
            return T;
        }

        public static string InterceptStr(string str, int length)
        {
            str = str.Trim();
            int x = str.Length;
            int y = 0;
            if (x >= 1)
            {
                for (int i = 1; i <= x; i++)
                {
                    Encoding ascii = Encoding.ASCII;
                    Byte[] encodedBytes = ascii.GetBytes(str.Substring(i - 1, 1));
                    if (encodedBytes[0] > 255)
                    {
                        y += 2;
                    }
                    else
                    {
                        y += 1;
                    }
                    if (y >= length)
                    {
                        str = str.Substring(0, i);
                        break;
                    }
                }
                return str;
            }
            return "";
        }

        /// <summary>
        /// 去除表格HTML标记
        /// </summary>
        /// <param name="htmlstring"></param>
        /// <returns>去掉表格HTML标记后的文本</returns>
        private static string WipeOffTableHTML(string htmlstring)
        {
            //删除脚本
            htmlstring = Regex.Replace(htmlstring, @"<[^>]*?>.*?</>", "", RegexOptions.IgnoreCase);
            //删除表格HTML
            htmlstring = Regex.Replace(htmlstring, @"</?table[^>]*>", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"</?tr[^>]*>", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"</?td[^>]*>", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"</?th[^>]*>", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"</?BLOCKQUOTE[^>]*>", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"</?tbody[^>]*>", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"<style[^\s]*", "", RegexOptions.IgnoreCase);
            return htmlstring;
        }

        ///   <summary>   
        ///   去除HTML标记   
        ///   </summary>
        /// <returns>已经去除后的文字</returns>   
        public static string NoHTML(string htmlstring)
        {
            //删除脚本   
            htmlstring = Regex.Replace(htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            //删除HTML   
            htmlstring = Regex.Replace(htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"([/r/n])[/s]+", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(quot|#34);", "/", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(nbsp|#160);", "   ", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(iexcl|#161);", "/xa1", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(cent|#162);", "/xa2", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(pound|#163);", "/xa3", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(copy|#169);", "/xa9", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&#(/d+);", "", RegexOptions.IgnoreCase);
            htmlstring = htmlstring.Replace("<", "");
            htmlstring = htmlstring.Replace(">", "");
            htmlstring = htmlstring.Replace("/r/n", "");
            htmlstring = htmlstring.Replace("\t", "");
            htmlstring = htmlstring.Replace("\r", "");
            htmlstring = htmlstring.Replace("\n", "");
            return htmlstring;
        }
    }
}